package com.isesol.ime.rest.api;

import com.google.common.collect.Maps;
import com.isesol.api.rest.annoations.RestServiceClass;
import com.isesol.api.rest.annoations.RestServiceMethod;
import com.isesol.api.rest.model.base.BusinessResources;
import com.isesol.arch.common.model.Result;
import com.isesol.ime.dto.SubFeedbackDTO;
import com.isesol.ime.rest.model.param.MachineToolSubFeedbackRequest;
import com.isesol.ime.service.SubFeedbackService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 订阅反馈
 *
 * @author Wang Jun
 */
@Service
@RestServiceClass(url = "ime")
public class SubFeedbackResource extends BusinessResources {

    /**
     * 订阅管理 Manager
     */
    @Resource
    SubFeedbackService subFeedbackService;

    public SubFeedbackDTO assemblySubscriptionDTO(MachineToolSubFeedbackRequest request) {

        SubFeedbackDTO dto = new SubFeedbackDTO();

        dto.setSubId(request.getSubId());
        dto.setFeedback(request.getFeedback());

        return dto;
    }

    @Transactional
    @RestServiceMethod(url = "subFeedback/create", requestType = MachineToolSubFeedbackRequest.class)
    public Map create(MachineToolSubFeedbackRequest request) throws Exception {

        LOGGER.info("【订阅反馈】subFeedback/create接口调用,request={}", request);

        Map m = Maps.newHashMap();


        SubFeedbackDTO subFeedbackDTO = assemblySubscriptionDTO(request);

        Result result = subFeedbackService.create(subFeedbackDTO.getSubId());

        m.put("result", result);

        return m;
    }

    @Transactional
    @RestServiceMethod(url = "subFeedback/success", requestType = MachineToolSubFeedbackRequest.class)
    public void success(MachineToolSubFeedbackRequest request) throws Exception {

        LOGGER.info("【订阅反馈】subFeedback/success接口调用,request={}", request);

        SubFeedbackDTO subFeedbackDTO = assemblySubscriptionDTO(request);

        subFeedbackService.subSuccsee(subFeedbackDTO.getSubId());

    }

}
